﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
// 添加https  
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
// end添加https  
namespace ShunFengApi.Common
{
    public static class HttpHelper
    {      
        /// <summary>
        /// 发送POST请求（application/json）
        /// </summary>
        /// <param name="url"></param>
        /// <param name="jsonParam"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public static T PostJson<T>(string url, string jsonParam, string encode = "utf-8") where T : new()
        {
            T result = new T();
            string strURL = url;
            HttpWebRequest request = null;
            if (strURL.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.ServerCertificateValidationCallback =
                    new RemoteCertificateValidationCallback(CheckValidationResult);
                request = (HttpWebRequest)WebRequest.Create(strURL);
                request.ProtocolVersion = HttpVersion.Version10;
            }
            else
            {
                request = (HttpWebRequest) WebRequest.Create(strURL);
            }
            request.Method = "POST";
            request.ContentType = "application/json;charset=" + encode.ToUpper();
            string paraUrlCoded = jsonParam;
            byte[] payload;
            payload = Encoding.GetEncoding(encode.ToUpper()).GetBytes(paraUrlCoded);
            request.ContentLength = payload.Length;
            Stream writer = request.GetRequestStream();
            writer.Write(payload, 0, payload.Length);
            writer.Close();
            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
                string StrDate = "";
                string strValue = "";
                StreamReader Reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encode.ToUpper()));
                while ((StrDate = Reader.ReadLine()) != null)
                {
                    strValue += StrDate + "\r\n";
                }
                if (!string.IsNullOrEmpty(strValue))
                {
                    result = JsonConvert.DeserializeObject<T>(strValue);
                }
            }
            catch (Exception ex)
            {
                //LogHelper.WriteLog("发送POST请求（application/json）:URL:"+url+"；\r\n 异常信息：" + ex.ToString(), LogType.error);
            }
            return result;
        }
        // 添加https  
        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true; //总是接受       
        }
        // end添加https  
    }
}
